package com.wang.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.Topic;
import javax.jms.Queue;

/**
 * @Classname Producer
 * @Description TODO
 * @Date 2024/5/27 11:57
 * @Created by pc
 */
@Component
public class Producer {
    @Resource(name = "springboot.queue")
    private Queue queue;

    @Resource(name = "springboot.topic")
    private Topic topic;

    @Resource(name = "springboot.replyQueue")
    private Queue replyQueue;
    @Autowired
    private JmsTemplate jmsTemplate;

    /**
     * 发送消息，destination是发送到的目标队列，message是待发送的消息内容;
     */
    public void sendMessage(Destination destination, final String message){
        jmsTemplate.convertAndSend(destination,message);
    }

    /**
     * 发送队列消息
     */
    public void sendQueueMessage(final String message) {
        sendMessage(queue, message);
    }

    /**
     * 发送Topic消息
     */
    public void sendTopicMessage(final String message) {
        sendMessage(topic, message);
    }

    /*发送队列的消息回复*/
    public void sendQueueMessageReply(String message){
        sendMessage(replyQueue,message);
    }

    @JmsListener(destination = "replyTo.queue",containerFactory = "jmsQueueListenerContainerFactory")
    public void consumerMessage(final String text) {
        System.out.println("从replyTo.queue队列中收到的应答报文为：" + text);
    }


}
